package by.dkusch.aircompany.webapp.page.base.common;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

import by.dkusch.aircompany.model.tables.base.BaseEntity;
import by.dkusch.aircompany.services.AbstractService;
import by.dkusch.aircompany.webapp.page.base.BaseDetailLayout;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.base.utils.GrantsResolver;

public class CommonActionPanel<ID, Entity extends BaseEntity<ID>, P extends BaseLayout> extends Panel {

	private static final long serialVersionUID = 1L;

	public CommonActionPanel(String id, final AbstractService<ID, Entity> service, final Entity entity, final Class<P> page, final Panel parent) {
		super(id);
		Link<Void> linkEdit = new Link<Void>("edit") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new BaseDetailLayout<ID, Entity>(entity, ActionType.EDIT, new PageCreator() {

					private static final long serialVersionUID = 1L;

					@Override
					public Page createPage() {
						try {
							return page.newInstance();
						} catch (InstantiationException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
						return null;
					}
				}));

			}
		};
		linkEdit.setVisible(GrantsResolver.canDo(entity.getClass(), ActionType.EDIT));
		add(linkEdit);

		Link<Void> linkView = new Link<Void>("view") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new BaseDetailLayout<ID, Entity>(entity, ActionType.VIEW, new PageCreator() {

					private static final long serialVersionUID = 1L;

					@Override
					public Page createPage() {
						try {
							return page.newInstance();
						} catch (InstantiationException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
						return null;
					}
				}));

			}
		};
		linkView.setVisible(GrantsResolver.canDo(entity.getClass(), ActionType.VIEW));
		add(linkView);

		AjaxLink<Void> linkRemove = new ConfirmationLink<Void>("remove", "remove.cofirmation.message") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				service.delete(entity);
				info(getString("column.common.action.deleted"));
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(parent);
			}

		};
		linkRemove.setVisible(GrantsResolver.canDo(entity.getClass(), ActionType.REMOVE) && service.checkCanRemoveRecord(entity));
		add(linkRemove);

		AjaxLink<Void> linkArc = new AjaxLink<Void>("archive") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {

				entity.setArcFl(!((entity.getArcFl() == null) ? false : entity.getArcFl()));
				service.saveOrUpdate(entity);
				if (entity.getArcFl()) {
					info(getString("column.common.action.to.archive"));
				} else {
					info(getString("column.common.action.from.archive"));
				}
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(parent);
			}

		};
		linkArc.setVisible(GrantsResolver.canDo(entity.getClass(), ActionType.EDIT));
		add(linkArc);

	};
}
